using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using VSSDKAssist.Assets.Helpers;
using EnvDTE;
using VSSDKAssist.Assets.Actions.Base;
using System.Globalization;

namespace VSSDKAssist.Recipes.ConfigureVSPackage
{
	/// <summary>
	/// Action that adds/update the DefaultRegistryRoot attribute
	/// </summary>
	public class AddDefaultRegistryRootAttributeAction : ActionBase
	{
		private CodeClass codeClass;

		/// <summary>
		/// Gets or sets the code class.
		/// </summary>
		/// <value>The code class.</value>
		[Input(Required = true)]
		public CodeClass CodeClass
		{
			get { return codeClass; }
			set { codeClass = value; }
		}

		private string vsHive;

		/// <summary>
		/// Gets or sets the vs hive.
		/// </summary>
		/// <value>The vs hive.</value>
		[Input(Required = true)]
		public string VSHive
		{
			get { return vsHive; }
			set { vsHive = value; }
		}

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			string attName = "DefaultRegistryRoot";
			string attValue =
				string.Format(
					CultureInfo.CurrentCulture,
					"@\"{0}\"",
					this.vsHive);

			CodeAttribute defaultRegistryRootAtt = FileCodeModelHelper.GetAttribute(this.codeClass, attName);

			if(defaultRegistryRootAtt != null)
			{
				FileCodeModelHelper.UpdateAttribute(defaultRegistryRootAtt, attValue);
			}
			else
			{
				FileCodeModelHelper.AddAttribute(codeClass, attName, attValue, false);
			}
		}
	}
}